package com.fly.video.downloader.core.app;
import ohos.aafwk.ability.Ability;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.global.resource.ResourceManager;

import java.lang.reflect.Method;
import java.util.Optional;

/**
 * Class which describes system bar sizing and other characteristics for the current
 * device configuration.
 *
 */
public class SystemBarMetrics {

    static {
        // Android allows a system property to override the presence of the navigation bar.
        // Used by the emulator.
        // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
                sNavBarOverride = null;
            }
//        }
    }

    private static String sNavBarOverride;


    private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
    private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height";
    private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape";
    private static final String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width";
    private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar";

    private final boolean mTranslucentStatusBar;
    private final boolean mTranslucentNavBar;
    private final int mStatusBarHeight;
    private final int mActionBarHeight;
    private final boolean mHasNavigationBar;
    private final int mNavigationBarHeight;
    private final int mNavigationBarWidth;
    private final boolean mInPortrait;
    private final float mSmallestWidthDp;

    public SystemBarMetrics(Ability activity, boolean translucentStatusBar, boolean traslucentNavBar) {
        ResourceManager res = activity.getResourceManager();
        mInPortrait = (res.getConfiguration().direction == Configuration.DIRECTION_VERTICAL);
        mSmallestWidthDp = getSmallestWidthDp(activity);
        mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
        mActionBarHeight = getActionBarHeight(activity);
        mNavigationBarHeight = getNavigationBarHeight(activity);
        mNavigationBarWidth = getNavigationBarWidth(activity);
        mHasNavigationBar = (mNavigationBarHeight > 0);
        mTranslucentStatusBar = translucentStatusBar;
        mTranslucentNavBar = traslucentNavBar;
    }

//    @TargetApi(14)
    public int getActionBarHeight(Ability context) {
//        int result = 0;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//            TypedValue tv = new TypedValue();
//            context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
//            result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
//        }
        return 40;
    }

//    @TargetApi(14)
    public int getNavigationBarHeight(Ability context) {
//        Resources res = context.getResources();
//        int result = 0;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//            if (hasNavBar(context)) {
//                String key;
//                if (mInPortrait) {
//                    key = NAV_BAR_HEIGHT_RES_NAME;
//                } else {
//                    key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME;
//                }
//                return getInternalDimensionSize(res, key);
//            }
//        }
        return 40;
    }

//    @TargetApi(14)
    public int getNavigationBarWidth(Context context) {
//        Resources res = context.getResources();
//        int result = 0;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//            if (hasNavBar(context)) {
//                return getInternalDimensionSize(res, NAV_BAR_WIDTH_RES_NAME);
//            }
//        }
        return 40;
    }

//    @TargetApi(14)
    public boolean hasNavBar(Context context) {
//        Resources res = context.getResources();
//        int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");
//        if (resourceId != 0) {
//            boolean hasNav = res.getBoolean(resourceId);
//            // check override flag (see static block)
//            if ("1".equals(sNavBarOverride)) {
//                hasNav = false;
//            } else if ("0".equals(sNavBarOverride)) {
//                hasNav = true;
//            }
//            return hasNav;
//        } else { // fallback
//            return !ViewConfiguration.get(context).hasPermanentMenuKey();
//        }
        return false;
    }

    public int getInternalDimensionSize(ResourceManager res, String key) {
//        int result = 0;
//        int resourceId = res.getIdentifier(key, "dimen", "android");
//        if (resourceId > 0) {
//            result = res.getDimensionPixelSize(resourceId);
//        }
        return 40;
    }

    public float getSmallestWidthDp(Ability activity) {
        Optional<Display>
                display = DisplayManager.getInstance().getDefaultDisplay(activity);
        DisplayAttributes metrics = display.get().getAttributes();
        float widthDp = metrics.width / metrics.densityPixels;
        float heightDp = metrics.height / metrics.densityPixels;
        return Math.min(widthDp, heightDp);
    }

    /**
     * Should a navigation bar appear at the bottom of the screen in the current
     * device configuration? A navigation bar may appear on the right side of
     * the screen in certain configurations.
     *
     * @return True if navigation should appear at the bottom of the screen, False otherwise.
     */
    public boolean isNavigationAtBottom() {
        return (mSmallestWidthDp >= 600 || mInPortrait);
    }

    /**
     * Get the height of the system status bar.
     *
     * @return The height of the status bar (in pixels).
     */
    public int getStatusBarHeight() {
        return mStatusBarHeight;
    }

    /**
     * Get the height of the action bar.
     *
     * @return The height of the action bar (in pixels).
     */
    public int getActionBarHeight() {
        return mActionBarHeight;
    }

    /**
     * Does this device have a system navigation bar?
     *
     * @return True if this device uses soft key navigation, False otherwise.
     */
    public boolean hasNavigtionBar() {
        return mHasNavigationBar;
    }

    /**
     * Get the height of the system navigation bar.
     *
     * @return The height of the navigation bar (in pixels). If the device does not have
     * soft navigation keys, this will always return 0.
     */
    public int getNavigationBarHeight() {
        return mNavigationBarHeight;
    }

    /**
     * Get the width of the system navigation bar when it is placed vertically on the screen.
     *
     * @return The width of the navigation bar (in pixels). If the device does not have
     * soft navigation keys, this will always return 0.
     */
    public int getNavigationBarWidth() {
        return mNavigationBarWidth;
    }

    /**
     * Get the layout inset for any system UI that appears at the top of the screen.
     *
     * @param withActionBar True to include the height of the action bar, False otherwise.
     * @return The layout inset (in pixels).
     */
    public int getPixelInsetTop(boolean withActionBar) {
        return (mTranslucentStatusBar ? mStatusBarHeight : 0) + (withActionBar ? mActionBarHeight : 0);
    }

    /**
     * Get the layout inset for any system UI that appears at the bottom of the screen.
     *
     * @return The layout inset (in pixels).
     */
    public int getPixelInsetBottom() {
        if (mTranslucentNavBar && isNavigationAtBottom()) {
            return mNavigationBarHeight;
        } else {
            return 0;
        }
    }

    /**
     * Get the layout inset for any system UI that appears at the right of the screen.
     *
     * @return The layout inset (in pixels).
     */
    public int getPixelInsetRight() {
        if (mTranslucentNavBar && !isNavigationAtBottom()) {
            return mNavigationBarWidth;
        } else {
            return 0;
        }
    }

}